local Piece = require "packages.chess-games.xiangqi.core.piece"

local Bishop = Piece:subclass("Xiangqi.Bishop")

function Bishop:initialize(id)
  Piece.initialize(self, id)

  self.name = 'bishop'
  self.redStr = '相'
  self.blackStr = '象'
end

---@param board integer[][]
---@param fromX integer
---@param fromY integer
---@param x integer
---@param y integer
function Bishop:canMoveTo(board, fromX, fromY, x, y)
  local dx = x - fromX
  local dy = y - fromY

  -- 必须是斜走两格
  if math.abs(dx) ~= 2 or math.abs(dy) ~= 2 then
    return false
  end

  -- 塞象眼：中间格不能有子
  local eyeX = fromX + dx // 2
  local eyeY = fromY + dy // 2
  if board[eyeY][eyeX] ~= 0 then
    return false
  end

  -- 不过河
  local myColor = self:getColor()
  if myColor == "red" then
    -- 红方象不能到 y ≤ 5
    if y <= 5 then
      return false
    end
  else
    -- 黑方象不能到 y ≥ 6
    if y >= 6 then
      return false
    end
  end

  -- 目标位置若有己方棋子，不可走
  local targetId = board[y][x]
  if targetId ~= 0 then
    local targetColor = (targetId > 0) and "red" or "black"
    if myColor == targetColor then
      return false
    end
  end

  return true
end

return Bishop

